<?php
ob_start();
header('Content-type: text/html;charset=utf-8');
/*
	此文件主要功能如下：
		1.在数据库中添加新记录
		2.返回新加记录信息。JSON格式
		3.创建上传目录
	此文件主要在数据库中添加新的记录并返回文件信息
		如果存在则在数据库中添加一条相同记录。返回添加的信息
		如果不存在，则向数据库中添加一条记录。并返回此记录ID
	控件每次计算完文件MD5时都将向信息上传到此文件中
	@更新记录：
		2017-08-17 取消对redis的引用
		2017-05-29 增加块路径生成逻辑
		2014-08-12 完成逻辑。
*/
require '../../vendor/autoload.php';

use utils\PathTool;
use sql\SqlWhereMerge;
use sql\SqlExec;
use utils\WebBase;

function loadRootData($uid,$pathRel)
{    
    $swm = new SqlWhereMerge();
    $swm->equal("f_uid", $uid);
    $swm->equal("f_complete", 1);
    $swm->equal("f_deleted", 0);
    $swm->equal("f_fdChild", 0);
    $where = $swm->to_sql();
    
    $se = new SqlExec();
    $files = $se->page2("up7_files"
        , "f_id,f_pid,f_nameLoc,f_sizeLoc,f_lenLoc,f_time,f_pidRoot,f_fdTask,f_pathSvr,f_pathRel"
        , $where
        , "f_fdTask desc,f_time desc");
    
    $o = array();
    $o["count"] = count($files);
    $o["code"]=0;
    $o["msg"]="";
    $o["data"]=$files;
    
    echo(json_encode($o,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE));    
}

function loadChildData($uid,$pathRel)
{   
    $se = new SqlExec();
    $files = $se->page2("up7_files"
        , "f_id,f_pid,f_nameLoc,f_sizeLoc,f_lenLoc,f_time,f_pidRoot,f_fdTask,f_pathSvr,f_pathRel"
        , "f_uid='$uid' and f_complete=1 and f_deleted=0 and f_fdChild=1 and f_pathRel=CONCAT('$pathRel',f_nameLoc)"
        , "f_fdTask desc,f_time desc");
    
    $folders = $se->page2("up7_folders"
        , "f_id,f_nameLoc,f_pid,f_sizeLoc,f_time,f_pidRoot,f_pathRel"
        , "f_uid='$uid' and f_complete=1 and f_deleted=0 and f_pathRel=CONCAT('$pathRel',f_nameLoc)"
        , "f_time desc");
    
    //设置值
    for($i=0,$l=count($folders);$i<$l;$i++)
    {
        $folders[$i]["f_fdTask"] = true;
        $folders[$i]["f_fdChild"] = false;
    }
    
    //合并文件和目录
    $folders = array_merge($folders,$files);
    
    $o = array();
    $o["count"] = count($folders);
    $o["code"]=0;
    $o["msg"]="";
    $o["data"]=$folders;
    
    echo(json_encode($o,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE));           
}

$wb = new WebBase;
$uid = $wb->queryString("uid");
$pathRel = $wb->queryString("pathRel");
$pathRel = PathTool::url_decode($pathRel);
if(!PathTool::endsWith($pathRel,"/")) $pathRel .= "/";

if($pathRel == "/")
{
    loadRootData($uid,$pathRel);
} 
else
{
    loadChildData($uid,$pathRel);
}
?>